Workshop: ggplot2

Tag 1

Author

Arieja Farugie & Luca Schnatz

Published

April 11, 2023

Scales

Text Annotationen

Themes

Patchwork

# install.packages("patchwork")
library(patchwork)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.1          ✔ purrr   1.0.1     
✔ tibble  3.2.1          ✔ dplyr   1.1.1.9000
✔ tidyr   1.3.0          ✔ stringr 1.5.0     
✔ readr   2.1.3          ✔ forcats 0.5.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(here)
here() starts at /Users/luca/PowerFolders/Hiwi_Arbeit/DPPD/WoMepS_DataVizR
exercise_data <- read_rds(here("data/exercise_data/clean_exercise_data.rds"))
glimpse(exercise_data)
Rows: 3,471
Columns: 32
$ id_participant                       <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11…
$ geschlecht                           <fct> weiblich, weiblich, maennlich, ma…
$ alter                                <dbl> 33, 50, 56, 61, 59, 56, 66, 25, 5…
$ familienstand                        <fct> ledig, verheirat.zusam.leb., verh…
$ nettoeinkommen_frei                  <dbl> 1800, 1100, 2500, NA, 1000, NA, 1…
$ nettoeinkommen_binned                <fct> 1750 - 1999 euro, 1000 - 1124 eur…
$ aequivalenzeinkommen                 <dbl> 1800, 2467, 1750, 4167, 1000, 466…
$ konfession                           <fct> evang.ohne freikirch, keiner reli…
$ erwerb_1                             <fct> erwerbstaetig, erwerbstaetig, erw…
$ erwerb_2                             <fct> "erwerbstaetig", "erwerbstaetig",…
$ schulabschluss                       <fct> "hochschulreife", "mittlere reife…
$ berufstaetig                         <fct> hauptberufl.ganztags, hauptberufl…
$ berufliche_stellung                  <fct> "angestellter", "angestellter", "…
$ staatsbuergerschaft                  <fct> deutschland, deutschland, deutsch…
$ deutsch_staats                       <fct> "ja", "ja", "ja", "ja", "ja", "ja…
$ bundesland                           <fct> mecklenb.-vorpommern, thueringen,…
$ ost_west                             <fct> neue bundeslaender, neue bundesla…
$ subj_schichteinstufung               <fct> mittelschicht, mittelschicht, unt…
$ bmi                                  <dbl> 23.53, 28.23, 24.77, 38.87, 30.82…
$ ps_01_gehetzt                        <int> 4, 4, 3, 2, 1, 2, 4, 2, 1, 3, 3, …
$ ps_02_niedergeschlagen               <int> 1, 4, 2, 3, 1, 2, 2, 3, 1, 1, 1, …
$ ps_03_ausgeglichen                   <int> 2, 4, 2, 2, 1, 4, 4, 3, 2, 2, 1, …
$ ps_04_energetisch                    <int> 2, 4, 2, 3, 1, 5, 2, 2, 3, 3, 3, …
$ ps_05_schmerzen                      <int> 1, 3, 1, 4, 1, 1, 4, 1, 4, 3, 1, …
$ ps_06_einsam                         <int> 1, 2, 1, 2, 1, 1, 1, 1, 3, 2, 1, …
$ ps_07_gesundeinschr_koerperlich_allg <int> 1, 3, 2, 3, 1, 1, 3, 3, 1, 1, 1, …
$ ps_08_gesundeinschr_koerperlich_art  <int> 1, 3, 2, 3, 1, 1, 4, 3, 3, 1, 1, …
$ ps_09_gesundeinschr_seelisch_allg    <int> 1, 2, 2, 2, 1, 1, 3, 1, 1, 1, 1, …
$ ps_10_gesundeinschr_seelisch_art     <int> 1, 1, 2, 2, 1, 1, 4, 1, 1, 1, 1, …
$ ps_11_einschr_sozial                 <int> 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, …
$ lebenszufriedenheit                  <dbl> 8, 8, 5, 8, 8, 8, 8, 8, 10, 8, 8,…
$ psychological_distress               <int> 16, 31, 20, 29, 11, 20, 32, 21, 2…

Basic-Plots erstellen

p1 <- ggplot(
  data = exercise_data,
  mapping = aes(x = bmi)
  ) + 
  geom_histogram(bins = 50, fill = "lightgrey", color = "white") + 
  theme_light()

print(p1)
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).

p2 <- ggplot(
  data = exercise_data,
  mapping = aes(x = alter, y = bmi, color = geschlecht)
) + 
  geom_point(alpha = 0.2) + 
  geom_smooth(method = "lm", fullwidth = TRUE, color = "black") + 
  facet_wrap(~geschlecht) +
  ggsci::scale_color_npg() +
  theme_light()
Warning in geom_smooth(method = "lm", fullwidth = TRUE, color = "black"):
Ignoring unknown parameters: `fullwidth`
print(p2)
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).

p3 <- ggplot(
  data = exercise_data,
  mapping = aes(x = geschlecht, y = bmi, color = geschlecht)
) + 
  geom_jitter(width = 0.1, alpha = 0.2) +
  stat_summary(
    fun = mean,
    geom = "pointrange",
    fun.min = function(x) mean(x) - sd(x),
    fun.max = function(x) mean(x) + sd(x),
    color = "black"
  ) + 
  ggsci::scale_color_npg() +
  theme_light()

print(p3)
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).

p4 <- ggplot(
  data = exercise_data,
  aes(x = alter, fill = geschlecht)
  ) +
  geom_density(alpha = 0.5) +
  ggsci::scale_fill_npg() +
  theme_light()

print(p4)
Warning: Removed 3 rows containing non-finite values (`stat_density()`).

Basic Patchwork-Syntax

  • p1 + p2 um zwei Plots in horizontaler Ebene nebeneinander darzustellen (äquivalent: p1 | p2)
  • p1 / p2 um zwei Plots in der vertikalen Ebene unter/übereinander darzustellen
p1 + p2 # oder p1 | p2
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).

p1 / p2
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Removed 55 rows containing missing values (`geom_point()`).

  • beliebige Kombination aus + bzw. | und /
  • durch Klammerschreibweise können “komplexere” Kombinationen gebildet werden
(p1 + p3) / p2
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).

  • gemeinsame Legende:
(p1 | p3) / p2 
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).

(p1 | p3) / p2 + plot_layout(guides = "collect")
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).

  • Kontrolle der Breite und höhe der einzelnen Teilplots:
(p1 | p3) + plot_layout(widths  = c(3, 2))
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).

(p3 / p4) + plot_layout(heights = c(1, 2))
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Removed 52 rows containing missing values (`geom_point()`).
Warning: Removed 3 rows containing non-finite values (`stat_density()`).

(p1 + p3) / (p2 + p4) + plot_layout(widths = c(1, 2), heights = c(1, 3))
Warning: Removed 52 rows containing non-finite values (`stat_bin()`).
Warning: Removed 52 rows containing non-finite values (`stat_summary()`).
Warning: Removed 52 rows containing missing values (`geom_point()`).
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 55 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 55 rows containing missing values (`geom_point()`).
Warning: Removed 3 rows containing non-finite values (`stat_density()`).